using System;
using System.Collections.Generic;
using System.Text;

namespace ITDM
{
    static public class Tools
    {
        /// <summary>
        /// A static routine to open a registry path. Creates the registry path if it doesn't exists.
        /// </summary>
        /// <param name="registry_path">Path to open. Must begin with HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE.</param>
        /// <returns></returns>
        static public Microsoft.Win32.RegistryKey OpenRegistry(string registry_path)
        {
            string reg = registry_path;
            Microsoft.Win32.RegistryKey hk = null;
            if (registry_path.StartsWith("HKEY_CURRENT_USER\\"))
            {
                hk = Microsoft.Win32.Registry.CurrentUser;
                reg = reg.Substring(18);
            }
            if (registry_path.StartsWith("HKEY_LOCAL_MACHINE\\"))
            {
                hk = Microsoft.Win32.Registry.LocalMachine;
                reg = reg.Substring(19);
            }
            if (hk == null)
                throw new Exception("Error: The registry path must begin with HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE.");

            return hk.CreateSubKey(reg, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree);
        }
    }
}
